package model

import (
	"gpt-api/framework/db"
	"time"
)

type Users struct {
	db.BaseModel
	Username     string    `json:"username" gorm:"column:username;type:varchar(50);default:'';comment:用户名"`
	Gender       int       `json:"gender" gorm:"column:gender;type:int;default:0;comment:性别"`
	OpenID       string    `json:"open_id" gorm:"column:open_id;type:varchar(100);unique_index;not null;comment:微信openid"`
	Avatar       string    `json:"avatar" gorm:"column:avatar;type:varchar(255);default:'';comment:头像"`
	VipLevel     int       `json:"vip_level" gorm:"column:vip_level;type:int;default:0;comment:VIP等级,0:非VIP,1:VIP"`
	Phone        string    `json:"phone" gorm:"column:phone;type:varchar(20);default:'';comment:手机号"`
	Expired      time.Time `json:"expired" gorm:"column:expired;comment:VIP过期时间"`
	InviteCode   string    `json:"invite_code" gorm:"column:invite_code;type:varchar(30);default:'';comment:邀请码"`
	InviteUserID uint      `json:"invite_user_id" gorm:"column:invite_user_id;type:int;default:0;comment:邀请注册用户ID"`
	Balance      float64   `json:"balance" gorm:"column:balance;type:decimal(10,2);default:0;comment:用户余额"`
	Score        int       `json:"score" gorm:"column:score;type:int;default:0;comment:用户积分"`
}

func (Users) TableName() string {
	return "users"
}

func (u *Users) GetUserInfoByOpenId(database *db.DB, openId string) (Users, error) {
	var user Users
	err := database.Where("open_id = ?", openId).First(&user).Error
	return user, err
}

func (u *Users) GetUserInfoById(database *db.DB, id uint64) (Users, error) {
	var user Users
	err := database.Where("id = ?", id).First(&user).Error
	return user, err
}

// 根据邀请码查询用户信息
func (u *Users) GetUserInfoByInviteCode(database *db.DB, inviteCode string) (Users, error) {
	var user Users
	err := database.Where("invite_code = ?", inviteCode).First(&user).Error
	return user, err
}

func (u *Users) Create(database *db.DB) error {
	return database.Create(u).Error
}

func (u *Users) Update(database *db.DB) error {
	return database.Save(u).Error
}
